#include "mylib.h"
#include "rle.h"

void drawUncompressedImage(const u16* data);
void setAbsolutePixel(int offSet, u16 color);
const u16 rle4[2] = {38400, 32767};

int main(void)
{
    REG_DISPCNT = 1027;
  
    // Call this function with rle2 and rle1 these are HARDER test cases for you to ensure it works.  
    drawUncompressedImage(rle3);
    
    while(1);
}

void setAbsolutePixel(int offSet, u16 color)
{
	videoBuffer[offSet] = color;
} 

/*
    Draws an RLE encoded image uncompressed onto the videoBuffer.
    Remember you may only call setPixel when the run length is 2 or less
    You must use DMA if the run length is 3 or greater! (Heavy penalty if you don't follow these rules)
*/

void drawUncompressedImage(const u16* data)
{
	int counter = 0;
	u16 * runLen = (u16*)&data[0];
	u16 * color = (u16 *)&data[1];
	while(counter <= 38400)
	{
		if(*runLen == 1)
		{
			setAbsolutePixel(counter, *color);
			counter++;
		}
		else if(*runLen== 2)
		{
			setAbsolutePixel(counter, *color);
			setAbsolutePixel(counter+1, *color);
			counter = counter+2;
		}
		else
		{
			REG_DMA3SAD = (vu32)color;
			REG_DMA3DAD = (vu32)&videoBuffer[counter];
			REG_DMA3CNT = *runLen | DMA_SOURCE_FIXED | DMA_ON;
			counter += *runLen;
		}
		runLen = runLen +2;
		color = color +2;
	}
}

